package com.zrwl.poscloud.xo.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zrwl.poscloud.base.entity.UserAdminR;
import com.zrwl.poscloud.base.superI.SuperMapper;
import com.zrwl.poscloud.xo.vo.UserAdminRVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 用户管理员关联表 Mapper类
 *
 * @author 刘思童 AutoGenerate
 * @date 2021-06-01
 */
public interface UserAdminRMapper extends SuperMapper<UserAdminR> {
    @Select(" <select>" +
            "        select r.uid," +
            "               u.uid userUid," +
            "               u.avatar," +
            "               u.gender userGender," +
            "               r.admin_uid adminUid," +
            "               u.nick_name userNickName," +
            "               u.user_name userName," +
            "               a.nick_name adminNickName," +
            "               a.user_name adminUserName," +
            "               r.status" +
            "        from  t_user u left join t_user_admin_r r on u.uid = r.user_uid" +
            "                 left join t_admin a on r.admin_uid = a.uid" +
            "        where u.status != 0" +
            "        <if test='userAdminRVO.status == 0'>" +
            "            and r.status != 0" +
            "        </if>" +
            "        <if test='userAdminRVO.status == -1'>" +
            "            and r.status = 0" +
            "        </if>" +
            "        <if test='userAdminRVO.status != -1 and userAdminRVO.status != 0'>" +
            "            and r.status = #{userAdminRVO.status}" +
            "        </if>" +
            "        <if test='userAdminRVO.weChat != null'>" +
            "            and u.status = 1 and u.we_chat = #{userAdminRVO.weChat} and r.status = 1" +
            "        </if>" +
            "        <if test='userAdminRVO.keyword != null'>" +
            "            and (a.user_name like concat ('%',#{userAdminRVO.keyword},'%') or" +
            "                a.nick_name like  concat ('%',#{userAdminRVO.keyword},'%') or" +
            "                u.user_name like  concat ('%',#{userAdminRVO.keyword},'%') or" +
            "                u.nick_name like  concat ('%',#{userAdminRVO.keyword},'%') or" +
            "                a.mobile    like  concat ('%',#{userAdminRVO.keyword},'%') or" +
            "                u.mobile    like  concat ('%',#{userAdminRVO.keyword},'%'))" +
            "        </if>" +
            "        <if test='userAdminRVO.userUid != null'>" +
            "            and r.user_uid = #{userAdminRVO.userUid}" +
            "        </if>" +
            "        <if test='userAdminRVO.adminUid != null'>" +
            "            and r.admin_uid = #{userAdminRVO.adminUid}" +
            "        </if>" +
            "        order by r.update_time desc" +
            "    </select>")
    IPage<UserAdminRVO> getUserAdminRList(IPage page, @Param("userAdminRVO") UserAdminRVO userAdminRVO);

    @Update("<update>" +
            "        update t_user_admin_r" +
            "        set status = 2" +
            "        where status != 0 and user_uid = #{userUid}" +
            "    </update>")
    int updateUser(@Param("userUid") String userUid);
}
